package com.ds.home.database.dao;

import com.ds.common.JDSConstants;
import com.ds.common.logging.Log;
import com.ds.common.logging.LogFactory;
import com.ds.home.database.BaseHibernateDAO;
import com.ds.home.database.vo.AreaVO;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.query.NativeQuery;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for AreaVO
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.ds.home.database.vo.AreaVO
 * @author MyEclipse Persistence Tools
 */
public class AreaDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(JDSConstants.CONFIG_KEY, AreaDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String MEMO = "memo";
	public static final String PLACEID = "placeid";



	public  List<AreaVO> loadByWhere(String where ){
		NativeQuery<AreaVO> query = this.getSession().createNativeQuery("select * from ha_area "+where, AreaVO.class);
		List<AreaVO>  volist= query.getResultList();
		return volist;
	}


	public void delete(AreaVO persistentInstance) {
		log.debug("deleting AreaVO instance");
		try {
			getSession().getTransaction().begin();
			getSession().delete(persistentInstance);
			getSession().getTransaction().commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public AreaVO findById(String id) {
		log.debug("getting AreaVO instance with id: " + id);
		AreaVO instance =null;
		try {
			
			 instance = (AreaVO) getSession().get(
					"com.ds.home.database.vo.AreaVO", id);
			
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}finally{
			close();
		}
		return instance;
	}

	public List<AreaVO> findByExample(AreaVO instance) {
		log.debug("finding AreaVO instance by example");
		List results =null;
		try {
			 results = getSession()
					.createCriteria("com.ds.home.database.vo.AreaVO")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}finally{
			close();
		}
		return results;
	}

	public List<AreaVO> findByProperty(String propertyName, Object value) {
		log.debug("finding AreaVO instance with property: " + propertyName
				+ ", value: " + value);
		List queryList=null;
		try {
			String queryString = "from AreaVO as model where model."
					+ propertyName + "= ?0";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			queryList=queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}finally{
			close();
		}
		return queryList;
	}

	public List<AreaVO> findByName(Object name) {
		return findByProperty(NAME, name);
	}
	

	public List<AreaVO> findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}
	
	public List<AreaVO> findByPlaceId(Object placeId) {
		return findByProperty(PLACEID, placeId);
	}

	public List<AreaVO> findAll() {
		List queryList=null;
		log.debug("finding all AreaVO instances");
		try {
			String queryString = "from AreaVO";
			Query queryObject = getSession().createQuery(queryString);
			
			queryList= queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			close();
		}
		return  queryList;
	}

	public void update(AreaVO detachedInstance) {
		log.debug("update AreaVO instance");
		try {
			getSession().getTransaction().begin();
			getSession().update(detachedInstance);
			getSession().getTransaction().commit();
			log.debug("update successful");
		
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}finally{
			close();
		}
	}

	public void insert(AreaVO instance) {
		log.debug("insert AreaVO instance");
		try {
			getSession().getTransaction().begin();
			getSession().insert(instance);
			getSession().getTransaction().commit();
			log.debug("insert successful");
		} catch (RuntimeException re) {
			log.error("insert failed", re);
			throw re;
		}finally{
			close();
		}
	}

	
}